package com.adam.example.search;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.elasticsearch.core.search.HitsMetadata;
import co.elastic.clients.json.JsonData;
import com.adam.example.ElasticsearchClientHolder;
import com.adam.example.types.GeoCity;

import java.io.IOException;
import java.util.*;

public class SearchGeoPointExample {

    public static void main(String[] args) throws IOException {
        ElasticsearchClient client = ElasticsearchClientHolder.getINSTANCE().initializeAndGetClient();

        //按照GeoJSON格式组装polygon多边形数据。
        //GeoJSON中，坐标为[经度,纬度]
        Map<String, Object> geoPolygonMap = new HashMap<>();
        geoPolygonMap.put("type", "Polygon");
        List<List<Double>> ring = new ArrayList<>();
        ring.add(Arrays.asList(111.7519900000, 40.8414900000));
        ring.add(Arrays.asList(106.5507300000, 29.5647100000));
        ring.add(Arrays.asList(121.4737000000, 31.2303700000));
        geoPolygonMap.put("coordinates", Arrays.asList(ring));
        SearchRequest searchRequest = SearchRequest.of(builder -> builder
                        .index("test_geo_point")
                        .query(queryBuilder -> queryBuilder
//                                .geoDistance(geoDistanceQueryBuilder -> geoDistanceQueryBuilder
//                                        .field("location")
//                                        .location(geoLocationBuilder -> geoLocationBuilder
//                                                .latlon(latLonGeoLocationBuilder -> latLonGeoLocationBuilder
//                                                        .lat(39.0851000000).lon(117.1993700000)
//                                                )
//                                        )
//                                        .distance("200km")
//                                )
//                                .geoBoundingBox(geoBoundingBoxQueryBuilder -> geoBoundingBoxQueryBuilder
//                                        .field("location")
//                                        .boundingBox(geoBoundsBuilder -> geoBoundsBuilder
//                                                .tlbr(topLeftBottomRightGeoBoundsBuilder -> topLeftBottomRightGeoBoundsBuilder
//                                                        .topLeft(geoLocationBuilder -> geoLocationBuilder
//                                                                .text("38.4864400000, 106.2324800000")
//                                                        )
//                                                        .bottomRight(geoLocationBuilder -> geoLocationBuilder
//                                                                .text("28.6820200000, 117.1993700000")
//                                                        )
//                                                )
//                                        )
//                                )
                                //geo_polygon query已过时，用geo_shape替代
//                                .geoPolygon(geoPolygonQueryBuilder -> geoPolygonQueryBuilder
//                                                .field("location")
//                                                .polygon(geoPolygonPointsBuilder -> geoPolygonPointsBuilder
//                                                        .points(geoLocationBuilder -> geoLocationBuilder
//                                                                .text("40.8414900000, 111.7519900000")
//                                                        )
//                                                        .points(geoLocationBuilder -> geoLocationBuilder
//                                                                .text("29.5647100000, 106.5507300000")
//                                                        )
//                                                        .points(geoLocationBuilder -> geoLocationBuilder
//                                                                .text("31.2303700000, 121.4737000000")
//                                                        )
//                                                )
//                                )
                                 // geo_shape
                                        .geoShape(geoShapeQueryBuilder -> geoShapeQueryBuilder
                                                .field("location")
                                                .shape(geoShapeFieldQuerybuilder -> geoShapeFieldQuerybuilder
                                                        .shape(JsonData.of(geoPolygonMap))
                                                )
                                        )
                )
        );

        SearchResponse<GeoCity> searchResponse = client.search(searchRequest, GeoCity.class);
        HitsMetadata<GeoCity> hitsMetadata = searchResponse.hits();
        System.out.println("搜索到" + hitsMetadata.total().value() + "条数据");
        List<Hit<GeoCity>> geoCityHitList = hitsMetadata.hits();
        for(int i=0;i<geoCityHitList.size();i++) {
            Hit<GeoCity> geoCityHit = geoCityHitList.get(i);
            GeoCity geoCity = geoCityHit.source();
            System.out.println("文档[" + (i+1) + "][评分" + geoCityHit.score() + "]:" + geoCity);
        }

        ElasticsearchClientHolder.getINSTANCE().close();
    }

}
